package com.caiyouxi.advertise.controller;

import com.caiyouxi.advertise.model.SlideDetail;
import com.caiyouxi.advertise.service.SlideDetailService;
import com.caiyouxi.common.Constant;
import com.caiyouxi.common.bean.ResData;
import com.caiyouxi.common.control.BaseController;
import com.caiyouxi.common.exception.ErrorCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * 幻灯片
 */
@RestController
@RequestMapping("slide")
public class SlideController extends BaseController {

    @Autowired
    SlideDetailService slideDetailService;

    /**
     * 查询幻灯片
     */
    @RequestMapping("/get_list")
    public ResData get_slide_list(HttpServletResponse response, HttpServletRequest request, @RequestBody SlideDetail bean){
        if(null == bean.getPid()){ //幻灯片位置
            return failed(ErrorCode.SYS_PARAMTER_MISSING.getCode(),ErrorCode.SYS_PARAMTER_MISSING.getMsg()+"pid",null);
        }
        return slideDetailService.get_slide_list(bean);
    }

    /**
     * 添加幻灯片
     */
    @RequestMapping("/add")
    public ResData add_slide(HttpServletResponse response, HttpServletRequest request, @RequestBody SlideDetail bean){
        if(null == bean.getPid()){ //幻灯片位置
            return failed(ErrorCode.SYS_PARAMTER_MISSING.getCode(),ErrorCode.SYS_PARAMTER_MISSING.getMsg()+"pid",null);
        }
        return slideDetailService.add_slide(bean);
    }

    /**
     * 幻灯片上下架、排序、更新
     */
    @RequestMapping("/change")
    public ResData change(HttpServletResponse response, HttpServletRequest request, @RequestBody SlideDetail bean){

        return slideDetailService.update_slide(bean);
    }

    /**
     * 幻灯片删除
     */
    @RequestMapping("/delete")
    public ResData delete(HttpServletResponse response, HttpServletRequest request, @RequestBody SlideDetail bean){
        if(null == bean.getId()){
            return failed(ErrorCode.SYS_PARAMTER_MISSING.getCode(),ErrorCode.SYS_PARAMTER_MISSING.getMsg()+"id",null);
        }
        bean.setIs_delete(Constant.DELETED);
        return slideDetailService.update_slide(bean);
    }

    /**
     * 幻灯片详情
     */
    @RequestMapping("/info")
    public ResData get_slide_info(HttpServletResponse response, HttpServletRequest request, @RequestBody SlideDetail bean){
        if(null == bean.getId()){
            return failed(ErrorCode.SYS_PARAMTER_MISSING.getCode(),ErrorCode.SYS_PARAMTER_MISSING.getMsg(),null);
        }
        return slideDetailService.get_slide_info(bean);
    }

}
